#include <iostream>

#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>

#include "../header/fudpClient.hpp"

void testClient();

int main(int argc, const char * argv[]) {
    // testClient();

    // check input format, defult ip now for test
    // need just the file name
    if(argc<4)
        exit(1);

    const char* method=argv[1];

    //char* ip = argv[2];
    const char* ip = argv[2];
    const char* filename=argv[3];

    fudpClient fclient(ip);
    
    if(strcmp(method_get,method)==0) {
        cout<<"begin to get"<<endl;
        fclient.lget(filename);
    } else if(strcmp(method_send,method)==0){
        cout<<"begin to send"<<endl;
        fclient.lsend(filename);
    } else {
        cout<<"ERROR: invalid method!"<<endl;
        exit(1);
    }


    return 0;
}
void testClient(){

	const char* ip = fltp_server_ip;
	int port=fltp_port;

    std::cout << "ip=" << ip << " port="<<port << std::endl;

    int fd;

    fd = socket(PF_INET,SOCK_DGRAM , 0);
    assert(fd >= 0);

    struct sockaddr_in address;
    bzero(&address,sizeof(address));

    //转换成网络地址
    address.sin_port = htons(port);
    address.sin_family = AF_INET;
    //地址转换
    inet_pton(AF_INET, ip, &address.sin_addr);
    //发送数据
    const char* normal_data = "my boy!";
    if(sendto(fd, normal_data, strlen(normal_data),0,(struct sockaddr*)&address,sizeof(address)) < 0) 
    { 
      perror("Send File Name Failed:"); 
      exit(1); 
    } else {
		char buffer[50]={0};
		socklen_t addressLen = sizeof(address);
		if(recvfrom(fd, buffer, 50-1,0,(struct sockaddr*)&address, &addressLen) == -1) 
        { 
           perror("Receive Data Failed:");
        } else {
			printf("response: %s\n", buffer);
		}
	}
    close(fd);
}